﻿/*----------------------------------------------------------------
// Copyright (C) 2011 
// 版权所有。 
//
// 文件名：TplParse.cs
// 文件功能描述：
//
// 
// 创建标识：kenter 2011-10-19
// EMAIL:kenter1643@163.com
//
// 修改标识：
// 修改描述：
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;

namespace WeiBoUI.Template
{
    public class TplParse
    {
        public string TplPath { get; set; }
        public string TplText { get; set; }

        #region 初始化
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="tplPath"></param>
        public TplParse(string tplPath)
        {
            this.TplPath = tplPath;
            this.TplText = File.ReadAllText(TplPath);
        } 
        #endregion

        #region 设置普通变量
        /// <summary>
        /// 设置普通变量
        /// </summary>
        /// <param name="memberName"></param>
        /// <param name="memberValue"></param>
        /// <param name="code"></param>
        public void SetMember(string memberName, string memberValue, ref string code)
        {
            code = Regex.Replace(code, "\\$\\(" + memberName + "\\)", memberValue);
        }  
        #endregion

        #region 设置List属性
        /// <summary>
        /// 设置List属性
        /// </summary>
        /// <param name="propName"></param>
        /// <param name="propValue"></param>
        /// <param name="code"></param>
        public void SetProperte(string propName, string propValue, ref string code)
        {
            code = Regex.Replace(code, "\\$\\#\\(" + propName + "\\)", propValue);
        } 
        #endregion

        #region 设置List
        /// <summary>
        /// 设置List
        /// </summary>
        /// <param name="ls"></param>
        /// <param name="listTagName"></param>
        /// <param name="code"></param>
        public void SetList(List<ForeachObj> ls, string listTagName, ref string code)
        {
            string pattern = "\\#\\<\\(ForeachBegin\\,List\\=" + listTagName + "\\)\\>" +
                "(?<TagName>[\\s\\S]*?)\\#\\<\\(ForeachEnd\\)\\>";
            MatchCollection mcs = Regex.Matches(code, pattern, RegexOptions.Multiline);
            foreach (Match m in mcs)
            {
                string newCode = m.Value;

                StringBuilder forCode = new StringBuilder();
                foreach (ForeachObj forObj in ls)
                {
                    string codeContent = m.Result("${TagName}");
                    //codeContent = Regex.Replace(codeContent, "\r", "");
                    //codeContent = Regex.Replace(codeContent, "\n", "");
                    //codeContent = Regex.Replace(codeContent, "\t", "");
                    //codeContent = codeContent.Trim();

                    foreach (KeyValuePair<string, string> val in forObj._values.ToArray())
                    {
                        SetProperte(val.Key, val.Value, ref codeContent);
                    }

                    forCode.Append(codeContent);
                }

                code = Regex.Replace(code, pattern, forCode.ToString());
            }
        } 
        #endregion

        #region 设置IF条件
        /// <summary>
        /// 设置IF条件
        /// </summary>
        /// <param name="code"></param>
        public void SetIf(ref string code)
        {
            string pattern = "\\#\\<\\(IfBegin_(?<IfName>[\\s\\S]*?)\\,Bool\\=(?<BoolCondition>[\\s\\S]*?)\\)\\>";
            string pattern2= "";
            MatchCollection mcs = Regex.Matches(code, pattern, RegexOptions.Multiline);
            foreach (Match m in mcs)
            {
                pattern2 = "\\#\\<\\(IfBegin_" + m.Result("${IfName}") + "\\,Bool\\=false\\)\\>"
                        + "(?<TagName>[\\s\\S]*?)\\#\\<\\(IfEnd_" + m.Result("${IfName}") + "\\)\\>";
                code = Regex.Replace(code, pattern2, "");
            }

            code = Regex.Replace(code, "\\#\\<\\(IfBegin_(?<IfName>[\\s\\S]*?)\\,Bool\\=true\\)\\>", "");
            code = Regex.Replace(code, "\\#\\<\\(IfEnd_(?<IfName>[\\s\\S]*?)\\)\\>", "");
        } 
        #endregion

        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="path"></param>
        /// <param name="code"></param>
        public void Save(string path, string code)
        {
            File.WriteAllText(path, code);
        } 
        #endregion

        public void Test()
        {
            string code = this.TplText;
            //普通变量替换
            SetMember("EntityName", "WebSite", ref code);
            //循环替换
            List<ForeachObj> ls = new List<ForeachObj>();
            ForeachObj fObj = new ForeachObj();
            fObj["FieldType"] = "int";
            fObj["FieldName"] = "ChannelName";
            fObj["dbReaderVal"] = "Convert.ToInt32(dbReader[\"channelid\"])";
            ls.Add(fObj);

            SetList(ls, "EntityProps", ref code);
            SetList(ls, "EntityReader", ref code);

            Console.Write(code);
        }
    }
}
